package com.dwxt.cdms.controller;

import com.dwxt.cdms.entity.ChunkInfo;
import com.dwxt.cdms.entity.CustomerFile;
import com.dwxt.cdms.entity.dto.ChunkUploadInfo;
import com.dwxt.cdms.service.CustomerFileService;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.IDUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@RequestMapping("/cdms/file")
@RestController
public class CustomerFileController {

    @Autowired
    private CustomerFileService customerFileService;

    /**
     * 上传文件到本地---体检报告关联用户
     * @param file
     * @param customerId
     * @return
     * @throws IOException
     */
    @RequestMapping("/saveFileLocal")
    public BaseResult saveFileLocal(MultipartFile file,Integer customerId) throws IOException {
        String fileUrl = customerFileService.saveFile(file, customerId);
        return BaseResult.ok(fileUrl);
    }

    /**
     * 上传文件到本地----资讯图片
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/saveFileLocalForInfo")
    public BaseResult saveFileLocal(MultipartFile file) throws IOException {
        String fileUrl = customerFileService.saveFileForInfo(file);
        return BaseResult.ok(fileUrl);
    }

    /**
     * showdoc
     * @catalog 视频上传
     * @title 文件分片上传，保存在本地
     * @method  post
     * @param chunkUploadInfo 必须 ChunkUploadInfo 分片文件上传对象
     * @description  文件上传
     * @url /cms/file/chunkUploadLocal
     * @return {"status":200,"msg":"SUCCESS","data":"文件路径"}
     */
    @RequestMapping("/chunkUploadLocal")
    public BaseResult chunkUpload(@Valid ChunkUploadInfo chunkUploadInfo) throws IOException {
//        if (chunkUploadInfo == null || StringUtils.isBlank(chunkUploadInfo.getId())) {
//            return BaseResult.build(StatusCode.PARAMETER_WRONG);
//        }
//        return customerFileService.chunkUpload(chunkUploadInfo);
        if (chunkUploadInfo == null || StringUtils.isBlank(chunkUploadInfo.getId())) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return customerFileService.chunkUploadToDFS(chunkUploadInfo);
    }

    /**
     * showdoc
     * @catalog 社区/文件上传
     * @title 获取分片信息
     * @method  post
     * @param task_id 必须 任务ID
     * @description  文件上传
     * @url /cms/file/chunkInfo
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158692026574007","chunk":null,"chunks":null,"fileName":null,"createTime":null,"dir":null,"offset":null,"fdfsFullPath":null,"size":null,"name":null,"fdfsPath":null,"fdfsGroup":null}}
     */
    @RequestMapping("/chunkInfo")
    public BaseResult chunkInfo(String task_id){
        if (StringUtils.isBlank(task_id)) {
            ChunkInfo chunkInfo = new ChunkInfo();
            chunkInfo.setId(IDUtils.genItemId());
            return BaseResult.ok(chunkInfo);
        }else {
            return customerFileService.getChunkInfo(task_id);
        }
    }

    /**
     * 查询健康报告文件地址
     * @param customerId
     * @return
     */
    @RequestMapping("/selectByCusId")
    public BaseResult selectByCusId(Integer customerId){
        CustomerFile file=customerFileService.selectByCusId(customerId);
        return BaseResult.ok(file);
    }
}
